package top.jacktgq.itheima.netty.protocol_design.groupchat.rpc;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import top.jacktgq.itheima.netty.protocol_design.groupchat.message.RpcRequestMessage;
import top.jacktgq.itheima.netty.protocol_design.groupchat.protocol.MessageCodecSharable;
import top.jacktgq.itheima.netty.protocol_design.groupchat.protocol.ProtocolFrameDecoder;

/**
 * @Author CandyWall
 * @Date 2021/3/23--14:49
 * @Description
 */
@Slf4j
public class RpcClient {
    public static void main(String[] args) throws InterruptedException {
        LoggingHandler loggingHandler = new LoggingHandler(LogLevel.DEBUG);
        MessageCodecSharable messageCodec = new MessageCodecSharable();
        RpcResponseMessageHandler rpcResponseMessageHandler = new RpcResponseMessageHandler();

        NioEventLoopGroup group = new NioEventLoopGroup();
        ChannelFuture channelFuture = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new ProtocolFrameDecoder())
                                .addLast(loggingHandler)
                                .addLast(messageCodec)
                                .addLast(rpcResponseMessageHandler);
                    }
                })
                .connect("localhost", 8080);

        // 监听客户端连接事件
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                log.debug("RPC客户端连接成功... {}" + channelFuture.channel());
                // 监听客户端SocketChannel关闭事件，关闭客户端事件循环组中的事件循环
                Channel channel = channelFuture.channel();
                channel.closeFuture().addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        // 客户端关闭
                        log.debug("客户端正在关闭...");
                        group.shutdownGracefully();
                        log.debug("客户端关闭成功！");
                    }
                });

                RpcRequestMessage message = new RpcRequestMessage(
                        1,
                        "top.jacktgq.itheima.netty.protocol_design.groupchat.server.service.HelloService",
                        "sayHello",
                        String.class,
                        new Class[]{String.class},
                        new Object[]{"张三"}
                );
                channel.writeAndFlush(message);
            }
        });

    }
}
